/*
 * script for asynchronous post and get 
 * 
 *no attempt has been made to support Internet Explorer version 6 and below
 *trhough the detection of the XMLHttpRequest identifier and instantiation of ActiveXObject objects
 *as the application is not intended for a general public audience
 *but for a specific audience of organisational members
 */
function getasync(url, element, isAsync) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				document.getElementById(element).innerHTML = xmlhttp.responseText;
			} else {
				alert("AJAX error: " + xmlhttp.status);
			}
		}
	}

	xmlhttp.open("GET", url, isAsync);
	xmlhttp.send();
}

//post accepting either data or the name of an HTML form containing to be parsed
function postasync(url, source, elements, isAsync) {
	var dataToPost = "";
	// if the source values are the contents of a html form
	if (source == "form") {

		// loop through the array and get values from
		// form elements to produce the data
		for ( var i = 0; i < elements.length; i++) {
			dataToPost += elements[i] + "="
					+ document.getElementById(elements[i]).value;
			if (i < elements.length - 1) {
				dataToPost += "&";
			}
		}
	}
	// if the data is a single key/value pair e.g. colour=blue to be posted
	else if (source = "value") {
		dataToPost = elements;
	}
	var xmlhttp = new XMLHttpRequest();

	// post the data
	xmlhttp.open("POST", url, isAsync);
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", dataToPost.length);
	xmlhttp.setRequestHeader("Connection", "close");
	var result = "";
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				result = xmlhttp.responseText;
			} else {
				alert("AJAX error: " + xmlhttp.status);
			}
		}
	}
	xmlhttp.send(dataToPost);
	return result;

}
